package driver

import (
	"errors"
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
	"strconv"
)

type OrderHandler struct {
	*handler.Handler
	orderService service.OrderService
}

func NewOrderHandler(
	handler *handler.Handler,
	orderService service.OrderService,
) *OrderHandler {
	return &OrderHandler{
		Handler:      handler,
		orderService: orderService,
	}
}

// GetOrderStatus 获取订单状态
func (h *OrderHandler) GetOrderStatus(ctx *gin.Context) {
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	orderStatus, err := h.orderService.GetOrderStatus(ctx, orderID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderStatus)
}


// FindNewOrderQueueData 查询司机新订单数据
func (h *OrderHandler) FindNewOrderQueueData(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderList, err := h.orderService.FindNewOrderQueueData(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderList)
}

// RobNewOrder 司机抢单
func (h *OrderHandler) RobNewOrder(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	flag, err := h.orderService.RobNewOrder(ctx, driverID, int64(orderID))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	if !flag {
		v1.HandleError(ctx, v1.StatusServiceErr, v1.ErrRobNewOrderFail, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// SearchDriverCurrentOrder 司机查找当前订单
func (h *OrderHandler) SearchDriverCurrentOrder(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderInfoList, err := h.orderService.SearchDriverCurrentOrder(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderInfoList)
}

// GetOrderInfo 获取订单账单详情信息
func (h *OrderHandler) GetOrderInfo(ctx *gin.Context) {
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	driverID := ctx.GetInt64("ID")
	orderInfoVo, err := h.orderService.GetDriverOrderInfo(ctx, orderID, driverID)
	if err != nil {
		if errors.Is(err, v1.ErrIllegalRequest) {
			v1.HandleError(ctx, v1.IllegalRequest, v1.ErrIllegalRequest, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderInfoVo)
}

// CalculateDrivingLine 计算最佳驾驶线路
func (h *OrderHandler) CalculateDrivingLine(ctx *gin.Context) {
	req := new(v1.CalculateDrivingLineForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	drivingLineVo, err := h.orderService.CalculateDrivingLine(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, drivingLineVo)
}

// DriverArriveStartLocation 司机到达代驾起始地点
func (h *OrderHandler) DriverArriveStartLocation(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	flag, err := h.orderService.DriverArriveStartLocation(ctx, orderID, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// UpdateOrderCart 更新代驾车辆信息
func (h *OrderHandler) UpdateOrderCart(ctx *gin.Context) {
	req := new(v1.UpdateOrderCartForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	driverID := ctx.GetInt64("ID")
	req.DriverID = driverID
	flag, err := h.orderService.UpdateOrderCart(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// StartDrive 开始代价服务
func (h *OrderHandler) StartDrive(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	startDriveForm := new(v1.StartDriveForm)
	if err := ctx.ShouldBindJSON(startDriveForm); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	startDriveForm.DriverID = int(driverID)
	flag, err := h.orderService.StartDrive(ctx, startDriveForm)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// EndDrive 结束代驾服务更新订单账单
func (h *OrderHandler) EndDrive(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderStrFeeForm := new(v1.OrderStrFeeForm)
	if err := ctx.ShouldBindJSON(orderStrFeeForm); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}

	otherFee, err1 := strconv.ParseFloat(orderStrFeeForm.OtherFee, 32)
	tollFee, err2 := strconv.ParseFloat(orderStrFeeForm.TollFee, 32)
	parkingFee, err3 := strconv.ParseFloat(orderStrFeeForm.ParkingFee, 32)
	if err1 != nil || err2 != nil || err3 != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}

	orderFeeForm := &v1.OrderFeeForm{
		OrderID:    orderStrFeeForm.OrderID,
		DriverID: driverID,
		OtherFee: otherFee,
		TollFee: tollFee,
		ParkingFee: parkingFee,
	}
	flag, err := h.orderService.EndDrive(ctx, orderFeeForm)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// FindDriverOrderPage 获取司机订单分页列表
func (h *OrderHandler) FindDriverOrderPage(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	pageStr := ctx.Param("page")
	limitStr := ctx.Param("limit")
	page, err := strconv.Atoi(pageStr)
	limit, err2 := strconv.Atoi(limitStr)
	if err != nil || err2 != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	orderPageVo, err := h.orderService.FindDriverOrderPage(ctx, driverID, page, limit)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderPageVo)
}

// SendOrderBillInfo 司机发送账单信息
func (h *OrderHandler) SendOrderBillInfo(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	flag, err := h.orderService.SendOrderBillInfo(ctx, orderID, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)

}










